package pt.nmfa.peste.server.services.devicemanagement;

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.Query;

import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Transactional;
import org.jboss.seam.annotations.remoting.WebRemote;

import pt.nmfa.peste.client.bean.ActionParametersDO;
import pt.nmfa.peste.client.bean.DeviceActionsDO;
import pt.nmfa.peste.client.bean.DeviceDO;
import pt.nmfa.peste.client.services.devicemanagement.DeviceManagement;
import pt.nmfa.peste.entity.ActionParameters;
import pt.nmfa.peste.entity.Device;
import pt.nmfa.peste.entity.DeviceActions;
import pt.nmfa.peste.scrappers.file.LircConfigScrapper;
import pt.nmfa.peste.server.services.util.PesteAction;

@Name("pt.nmfa.peste.client.services.devicemanagement.DeviceManagement")
@Stateless
public class DeviceManagementActions  extends PesteAction implements DeviceManagement{

	@In(value = "entityManager")
	private EntityManager em;

	
	@WebRemote
	public void createDevice(DeviceDO deviceDO) {
		Device dev=new Device();
		copyObj(dev,deviceDO);
		dev.setDeviceId(null);		
		em.persist(dev);
		
	}
	@WebRemote
	@Transactional
	public void createDeviceActions(DeviceActionsDO deviceActionsDO) {
		Device device=em.find(Device.class, deviceActionsDO.getDeviceId());
		DeviceActions devActions=new DeviceActions();
		copyObj(devActions,deviceActionsDO);
		devActions.setDevice(device);
		devActions.setDeviceActionsId(null);
		em.persist(devActions);
	}
	@WebRemote
	public void deleteDevice(int deviceID) {
		
		
	}
	@WebRemote
	public void deleteDeviceActions(int deviceID) {
		// TODO Auto-generated method stub
		
	}
	@WebRemote
	public DeviceDO getDevice(int deviceID) {
		Device device=em.find(Device.class, deviceID);
		return (DeviceDO)convertObj(DeviceDO.class, device);
	}
	@WebRemote
	public List<DeviceActionsDO> getDeviceActionsList(int deviceID) {
		Device device=em.find(Device.class, deviceID);
	
//		Query query = em.createQuery("select c from DeviceActions c where c.device.deviceId = :deviceId");
//		query.setParameter("deviceId", deviceID);
		
		return convertSet(DeviceActionsDO.class, device.getDeviceActionses());
	}
	@WebRemote
	public List<ActionParametersDO> getDeviceActionsParametersList(int deviceActionsID) {

		DeviceActions deviceActions=em.find(DeviceActions.class, deviceActionsID);
		
		
		return convertSet(ActionParametersDO.class, deviceActions.getActionParameterses());
	}

	@WebRemote
	public List<DeviceDO> getDevices() {
		Query query = em.createQuery("select c from Device c");
		return convertList(DeviceDO.class, query.getResultList());
	}
	@WebRemote
	@Transactional
	public void updateDevice(DeviceDO deviceDO) {
		Device device=em.find(Device.class, deviceDO.getDeviceId());
		copyObj(device,deviceDO);
		em.persist(device);

	}
	@WebRemote
	@Transactional
	public void updateDeviceActions(DeviceActionsDO deviceActionsDO) {
		DeviceActions deviceActions=em.find(DeviceActions.class, deviceActionsDO.getDeviceActionsId());
		copyObj(deviceActions,deviceActionsDO);
		em.persist(convertObj(DeviceActions.class,deviceActionsDO));
		
	}
	@WebRemote
	@Transactional
	public void importLircConfig(int deviceId,String file)
	{
		LircConfigScrapper lircConfig=new LircConfigScrapper(file);

		Device device=em.find(Device.class, deviceId);
		
		
		List<String> codesList=lircConfig.getCodeList();
		for(int i=0;i<codesList.size();i++)
		{
			DeviceActions deviceActions=new DeviceActions(device,"IR","Botão "+codesList.get(i));
			em.persist(deviceActions);
			ActionParameters actionParameters=new ActionParameters(deviceActions,"name",lircConfig.getName());
			em.persist(actionParameters);
			actionParameters=new ActionParameters(deviceActions,"code",codesList.get(i));
			em.persist(actionParameters);
			System.out.println(codesList.get(i));
		}
		
		
	}
	
}
